package com.woniuxy.woniubook.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.woniubook.entity.Address;
import com.woniuxy.woniubook.mapper.AddressMapper;
import com.woniuxy.woniubook.service.AddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wugd
 * @since 2022-11-16 02:13:10
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {

    @Autowired
    AddressMapper addressMapper ;

    @Override
    public List<Address> getAddressByUser(Long id) {
        QueryWrapper<Address> queryWrapper = new QueryWrapper();
        queryWrapper.eq("userId",id);
        return  addressMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional
    public void add(Address address) {
        if ("1".equals(address.getIsDefault())){
            addressMapper.updateAddressByDefault(address.getUserId());
        }else {
            address.setIsDefault("0");
        }
        //添加
        addressMapper.insert(address);
    }
}
